package com.eastcom.providor.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import com.eastcom.bm.atom.Atom_MetalKpiBase;
import com.eastcom.bm.atom.Atom_NeType;
import com.eastcom.bm.atom.Atom_Type;
import com.eastcom.client.TreeNode;
import com.eastcom.providor.dao.IKpiTypeDao;
import com.eastcom.providor.dao.IMetalKpiBaseDao;
import com.eastcom.providor.dao.INeTypeDao;
import com.eastcom.providor.service.BaseService;

public class TreeNodeService extends BaseService{

	
	
    private IMetalKpiBaseDao metalKpiBaseDao;
	
    private INeTypeDao neTypeDao;

    private IKpiTypeDao kpiTypeDao;
	

	public void setKpiTypeDao(IKpiTypeDao kpiTypeDao) {
		this.kpiTypeDao = kpiTypeDao;
	}
	public void setNeTypeDao(INeTypeDao neTypeDao) {
		this.neTypeDao = neTypeDao;
	}
	public void setMetalKpiBaseDao(IMetalKpiBaseDao metalKpiBaseDao) {
		this.metalKpiBaseDao = metalKpiBaseDao;
	}
	

	public List<TreeNode> getChildNodesByParentId(String parentId){
		List<TreeNode> childNodes=new ArrayList<TreeNode>();
		String[] ids = parentId.split("&");
		log.info("拿指标树图:"+parentId);
		try{
			if(parentId.equals("KPITreeRoot")){
				List<Atom_NeType> service =neTypeDao.selectAllNeType();
				for(Atom_NeType se:service){
					TreeNode node = new TreeNode();
					node.setId("1&bs&"+se.getId());
					node.setText(se.getcName());
					node.setExpanded(false);
					node.setLeaf(false);
					node.setIconCls("sys_server");
					childNodes.add(node);
				}
			}
			else if(ids.length==3){
				List<Atom_Type> type =kpiTypeDao.selectTypeNoParent();
				for(Atom_Type se:type){
					TreeNode node = new TreeNode();
					node.setId("2&tp&"+ids[2]+"&"+se.getId());
					node.setText(se.getCname());
					node.setExpanded(false);
					node.setLeaf(false);
					node.setIconCls("sys_type");
					childNodes.add(node);
				}
			}
			else if(ids.length==4){
				String businessType=ids[2];
				String kpiType =ids[3];
				boolean initialExpand =false;
				List<Atom_MetalKpiBase> kpi = metalKpiBaseDao.selectMetalKpiBaseByBusinessKpiType(businessType, kpiType,null, initialExpand);
				initialRelatedKpiName(kpi);
				for(Atom_MetalKpiBase se:kpi){
					TreeNode node = new TreeNode();
					node.setId("3&kpi&"+ids[2]+"&"+ids[3]+"&"+se.getId());
					node.setText(se.getCn_name());
					node.setExpanded(false);
					node.setLeaf(true);
					node.setRelatedKpis(se.getRelatedKpis());
					node.setDesc(se.getDesc());
					node.setType(se.getType());
					node.setBusinessId(se.getBusinessId());
					node.setQtip(se.getCn_name()+"<BR>"+se.getType()+"<BR>"+(se.getDesc()==null?"暂无说明":se.getDesc()));
					//node.setObject(se);
					node.setIconCls("sys_kpi");
					childNodes.add(node);
				}
			}
		}
		catch(Exception e){
			log.error("TreeNodeService getChildNodesByParentId error", e);
		}
		return childNodes;
	}
	
	public List<TreeNode> getChildPublicNeNodesByParentId(String parentId){
		List<TreeNode> childNodes=new ArrayList<TreeNode>();
		log.info("拿指标树图:"+parentId);
		try{
			
		}
		catch(Exception e){
			log.error("TreeNodeService getChildPublicNeNodesByParentId error", e);
		}
		//JDBCTemplate.doSessionFinally();
		
		return childNodes;
	}
	
	private String getTreenodeId(){
		return "";
	}
	
	public void initialRelatedKpiName(List<Atom_MetalKpiBase> kpiBases) throws Exception{
		if(kpiBases==null||kpiBases.isEmpty())
			return;
		
		Set<String> ids=new HashSet<String>();
		Map<Atom_MetalKpiBase,String[]> kpiBaseRelatedKpis=new HashMap<Atom_MetalKpiBase,String[]> ();
		for(Atom_MetalKpiBase kpBase:kpiBases){
			if(kpBase.getRelatedKpis()==null||"".equals(kpBase.getRelatedKpis().trim()))
				continue;
			
			String[] tempIds=kpBase.getRelatedKpis().split(",");
			kpiBaseRelatedKpis.put(kpBase, tempIds);
			for(String kpi:tempIds)
				ids.add(kpi);
		}
		
		if(kpiBaseRelatedKpis.isEmpty())
			return;
		
		Map<Object,Map> kpiNameMap=metalKpiBaseDao.getKpiNameById(ids);
		if(kpiNameMap==null||kpiNameMap.isEmpty())
			return;
		
		for(Atom_MetalKpiBase kpiBase:kpiBaseRelatedKpis.keySet()){
			String kpiIdName="";
			String[] relatedKpis=kpiBaseRelatedKpis.get(kpiBase);
			for(String kpi:relatedKpis){
				String name=kpiNameMap.get(kpi)==null||kpiNameMap.get(kpi).get("kpi_name_cn")==null?" ":(String)kpiNameMap.get(kpi).get("kpi_name_cn");
				kpiIdName+=kpi+"&"+name+"@#";
			}
			
			kpiBase.setRelatedKpis(kpiIdName);
		}
	}
}
